package com.migo.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.mail.internet.InternetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

@Service
public class AppMailUtil {

    @Autowired
    StringRedisTemplate redisTemplate;

    public static final String SENDNAME = "PowerVision";
    public static final int OUTDATETIME = 43200;

    @SuppressWarnings("static-access")
    public boolean sendMail(String VerificationCode, String charset, String userMail, String actionType) throws Exception {
        String subject = "";
        String message = "";
        boolean flag = false;
        try {
            List<InternetAddress> list = new ArrayList<InternetAddress>();
            list.add(new InternetAddress(userMail));
            if (charset.equals("jp")||charset.equals("ja")) {
                if (actionType.equals("1")) {//注册邮件
                    subject = "本人確認のご案内";
                    message = "認証コード: " + VerificationCode + "";
                    redisTemplate.boundValueOps(userMail).set(VerificationCode);
//                                                jedis.set(userMail, VerificationCode);

//					jedis.expire(userMail, OUTDATETIME);
                    redisTemplate.boundValueOps(userMail).expire(OUTDATETIME, TimeUnit.SECONDS);
                } else if (actionType.equals("2")) {//重置密码邮件
                    subject = "パスワード再設定のお知らせ";
                    message = "認証コード: " + VerificationCode + "";
                    redisTemplate.boundValueOps(userMail).set(VerificationCode);
                    redisTemplate.boundValueOps(userMail).expire(OUTDATETIME, TimeUnit.SECONDS);
                } else if (actionType.equals("3")) {//绑定邮箱邮件
                    subject = "メールアドレス登録確認のお知らせ";
                    message = "認証コード: " + VerificationCode + "";
                    redisTemplate.boundValueOps(userMail).set(VerificationCode);
                    redisTemplate.boundValueOps(userMail).expire(OUTDATETIME, TimeUnit.SECONDS);
                } else {
                    return false;
                }
            }

            if (charset.equals("en")) {
                if (actionType.equals("1")) {//注册邮件
                    subject = "Mail registration verification";
                    message = "Your verification code is: " + VerificationCode + "";
                    redisTemplate.boundValueOps(userMail).set(VerificationCode);
//                                                jedis.set(userMail, VerificationCode);

//					jedis.expire(userMail, OUTDATETIME);
                    redisTemplate.boundValueOps(userMail).expire(OUTDATETIME, TimeUnit.SECONDS);
                } else if (actionType.equals("2")) {//重置密码邮件
                    subject = "Retrieve password verification mail";
                    message = "Your verification code is: " + VerificationCode + "";
                    redisTemplate.boundValueOps(userMail).set(VerificationCode);
                    redisTemplate.boundValueOps(userMail).expire(OUTDATETIME, TimeUnit.SECONDS);
                } else if (actionType.equals("3")) {//绑定邮箱邮件
                    subject = "Mailbox binding mail";
                    message = "Your verification code is: " + VerificationCode + "";
                    redisTemplate.boundValueOps(userMail).set(VerificationCode);
                    redisTemplate.boundValueOps(userMail).expire(OUTDATETIME, TimeUnit.SECONDS);
                } else {
                    return false;
                }
            } else if (actionType.equals("1")) {//注册邮件
                subject = "邮箱注册验证邮件";
                message = "你的验证码为: " + VerificationCode + "";
                redisTemplate.boundValueOps(userMail).set(VerificationCode);
                redisTemplate.expire(userMail, OUTDATETIME, TimeUnit.SECONDS);
//                jedis.set(userMail, VerificationCode);
//                jedis.expire(userMail, OUTDATETIME);
            } else if (actionType.equals("2")) {//重置密码邮件
                subject = "找回密码验证邮件";//找回密码验证邮件
                message = "你的验证码为: " + VerificationCode + "";
                redisTemplate.boundValueOps(userMail).set(VerificationCode);
                redisTemplate.expire(userMail, OUTDATETIME, TimeUnit.SECONDS);
//                jedis.set(userMail, VerificationCode);
//                jedis.expire(userMail, OUTDATETIME);
            } else if (actionType.equals("3")) {//绑定邮箱邮件
                subject = "邮箱绑定邮件";
                message = "Your verification code is: " + VerificationCode + "";
                redisTemplate.boundValueOps(userMail).set(VerificationCode);
                redisTemplate.boundValueOps(userMail).expire(OUTDATETIME, TimeUnit.SECONDS);
            } else {
                return false;
            }
            if("zh".equals(charset))
            {
                flag = MailUtil.sendUserMail(subject, message, list, SENDNAME);
            }
            else {

                ExecutorService threadPool = ThreadPoolUtils.getThreadPool();
                threadPool.execute(new HttpMailUtilRunable("http://api.sms.powervision.me/api/sms/sendAliyunEmail", userMail, VerificationCode, "8.209.72.56"));
//                HttpClientUtils.sendMail(url,userMail,VerificationCode);
//                String url = "http://47.242.207.218:8888/sendRandomMail";
//                ExecutorService threadPool = ThreadPoolUtils.getThreadPool();
//                threadPool.execute(new HttpMailUtilRunable(url,userMail, VerificationCode));
                flag = true;
            }
            return flag;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
//            jedisPool.returnBrokenResource(jedis);
            return flag;
        } finally {
//            jedisPool.returnBrokenResource(jedis);

        }
    }

    public static void main(String[] args) {
        HttpClientUtils.sendMail("http://api.sms.powervision.me/api/sms/sendAliyunEmail", "wes.wang@showdao.com","123456", "8.209.72.56");
    }
}
